package com.easylinkin.linkappapi.deviceservice.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.deviceservice.entity.DeviceServices;
import com.easylinkin.linkappapi.deviceservice.service.DeviceServiceService;
import com.easylinkin.linkappapi.operatelog.LogHelper;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author TongJie
 * @since 2020-04-15
 */
@RestController
@RequestMapping("/deviceService")
@Api(value="设备服务",tags={"设备服务"})
@Slf4j
public class DeviceServicesController {

    @Resource
    private DeviceServiceService deviceServiceService;

    @ApiOperation("根据设备型号ID等查询条件获取设备服务list")
    @PostMapping("getDeviceServicesList")
    public RestMessage getDeviceServicesList(@RequestBody DeviceServices deviceServices) {
        return RestBuilders.successBuilder().data(deviceServiceService.getDeviceServicesList(deviceServices)).build();
    }

    /**
     * 小程序 报错接口找不到问题修改 和getDeviceServicesList接口一样的
     */
    @PostMapping("getDeviceServiceData")
    public RestMessage getDeviceServiceData(@RequestBody DeviceServices deviceServices) {
        return RestBuilders.successBuilder().data(deviceServiceService.getDeviceServicesList(deviceServices)).build();
    }

    @ApiOperation("根据服务ID批量修改设备服务")
    @PostMapping("/update")
    public RestMessage update(@RequestBody List<DeviceServices> deviceServices) {
        Assert.noNullElements(deviceServices, "设备服务参数列表为空");
        return RestBuilders.successBuilder().data(deviceServiceService.updateBatchById(deviceServices)).build();
    }

    @ApiOperation("根据设备服务ID修改设备服务")
    @PostMapping("/updateDeviceService")
    public RestMessage updateDeviceService(@RequestBody DeviceServices deviceService) {
        Assert.notNull(deviceService, "设备服务为空");
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", deviceService.getId());
        if(null != deviceService.getIsShow()) {
        	updateWrapper.set("is_show", deviceService.getIsShow());
        }
        return RestBuilders.successBuilder().data(deviceServiceService.updateById(deviceService)).build();
    }

    @ApiOperation("下发设备服务指令")
    @PostMapping("send")
    @CommonOperateLogAnnotate(module = LogModule.DOWNSEND_CONTROL, desc = "")
    public RestMessage send(@RequestBody DeviceServices deviceServices) {
        Assert.notNull(deviceServices, "参数为空");
        LogHelper.setContent(deviceServiceService.downSendLogMessage(deviceServices.getDownParameter()));
        return RestBuilders.successBuilder().data(deviceServiceService.send(deviceServices)).build();
    }

    @ApiOperation("批量下发设备服务指令")
    @PostMapping("batchSend")
    public RestMessage batchSend(@RequestBody DeviceServices deviceServices) {
        Assert.notNull(deviceServices, "参数为空");
        return RestBuilders.successBuilder().data(deviceServiceService.batchDeviceSend(deviceServices, true)).build();
    }
}
